//
// Copyright (C) OpenSim Ltd.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//

package inet.node.base;

import inet.common.MessageDispatcher;
import inet.networklayer.contract.INetworkLayer;
import inet.node.inet.Mmu;

module NetworkLayerNodeBase extends LinkLayerNodeBase
{
    parameters:
        bool hasIpv4 = default(true);
        bool hasGn = default(false); 
        bool forwarding = default(false);
        bool multicastForwarding = default(false);
        bool enablePFC = default(false);
        bool enableECN = default(false);
        bool needMmu = enablePFC || enableECN;
        bool infiniMemory = default(false);
        *.forwarding = forwarding;
        *.multicastForwarding = multicastForwarding;
        @figure[networkLayer](type=rectangle; pos=250,306; size=1000,130; fillColor=#00ff00; lineColor=#808080; cornerRadius=5; fillOpacity=0.1);
        @figure[networkLayer.title](type=text; pos=1245,311; anchor=ne; text="network layer");

    submodules:
        ipv4: <default("Ipv4NetworkLayer")> like INetworkLayer if hasIpv4 {
            parameters:
                @display("p=375,376;q=queue");
        }
        generic: <default("")> like INetworkLayer if hasGn {
            parameters:
                @display("p=675,376;q=queue");
        }
        nl: MessageDispatcher {
            parameters:
                @display("p=750,446;b=1000,5,,,,1");
        }
        mmu:Mmu if needMmu{
            parameters:
                @display("p=750,446;b=1000,5,,,,1");
                mmu.isInfiniMemory = infiniMemory;
        }

    connections allowunconnected:
        for i=0..sizeof(ethg)-1 {
            eth[i].upperLayerOut --> nl.in++ if needMmu==false;
            eth[i].upperLayerIn <-- nl.out++ if needMmu==false;

            nl.out++ --> mmu.upperLayerIn++ if needMmu==true;
            mmu.upperLayerOut++ --> nl.in++ if needMmu==true;
            eth[i].upperLayerOut --> mmu.lowerLayerIn++ if needMmu==true;
            eth[i].upperLayerIn <-- mmu.lowerLayerOut++ if needMmu==true;
        }   

        for i=0..numVlanInterfaces-1 {
            vlan[i].upperLayerOut --> nl.in++ if needMmu==false;
            vlan[i].upperLayerIn <-- nl.out++ if needMmu==false;

            nl.out++ --> mmu.upperLayerIn++ if needMmu==true;
            mmu.upperLayerOut++ --> nl.in++ if needMmu==true;
            vlan[i].upperLayerOut --> mmu.lowerLayerIn++ if needMmu==true;
            vlan[i].upperLayerIn <-- mmu.lowerLayerOut++ if needMmu==true;
        }
        
        ipv4.ifIn <-- nl.out++ if hasIpv4;
        ipv4.ifOut --> nl.in++ if hasIpv4;

        generic.ifIn <-- nl.out++ if exists(generic);
        generic.ifOut --> nl.in++ if exists(generic);
}
